package com.hyb.algorithm.data.struct.str;

/**
 * @author: ybhu
 * @create: 2021/09/29 02:30
 *
 * 125. 验证回文串
 * https://leetcode-cn.com/problems/valid-palindrome/
 *
 */
public class ValidPalindrome {

    /**
     * 双指针碰撞
     * @param s
     * @return
     */
    public boolean isPalindrome(String s) {

        int left=0;
        int right=s.length()-1;

        while (left<right){

            //如果不是字母或者数字
            while (left<right&&!Character.isLetterOrDigit(s.charAt(left))){
                left++;
            }

            //如果不是字母或者数字
            while(left<right&&!Character.isLetterOrDigit(s.charAt(right))){
                right--;
            }

            if(left>=right){
                return true;
            }

            if(Character.toLowerCase(s.charAt(left))!=Character.toLowerCase(s.charAt(right))){
                return  false;
            }

            left++;
            right--;
        }

        return true;
    }
}
